package com.itheima.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xgm
 * @date 2020/11/21
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;


    //分页查询
    @RequestMapping("/findPage")
    //@PreAuthorize("hasAuthority('Administrator')")
    public Result findPage(QueryPageBean queryPageBean){
        log.info("[权限-分页查询]data:{}",queryPageBean);
        //rpc查询数据
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        //封装返回数据
        return new Result(true,"查询成功",pageResult);
    }

    /**
     * 新增角色之权限选择
     *
     */
    @RequestMapping("/findAllPermission")
    public Result findAllPermission(){
        log.info("[新增角色之-----权限显示]");
        List<Permission> list = roleService.findAllPermission();
        return new Result(true,"权限查到了",list);
    }
    /**
     * 新增角色之菜单选择
     *
     */
    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){
        log.info("[新增角色之-----父菜单显示]");
        List<Menu> list = roleService.findAllMenu();
        log.info("[新增角色之-----菜单显示]list:[]",list);
        return new Result(true,"菜单查到了",list);
    }


    /**
     * 新增
     * @param role
     * @return
     */
    @RequestMapping("/add")
    //@PreAuthorize("hasAuthority('Permission_ADD')")
    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        log.info("[角色-新增]data:{},permissionIds:{},menuIds:{}",role,permissionIds,menuIds);
        //rpc请求添加
        roleService.add(role,permissionIds,menuIds);
        return new Result(true,"新增角色成功");
    }


    /**
     * 删除角色
     *
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        log.info("[删除角色]id:{}",id);
        roleService.delete(id);
        return new Result(true,"删除成功");
    }



    /**
     * 编辑角色之---角色先回显
     */
    @RequestMapping("/findRoleById")
    public Result findRoleById(Integer id){
        log.info("[编辑角色回显角色]id:{}",id);
        Role role = roleService.findRoleById(id);
        return new Result(true,"找到角色了",role);
    }

    /**
     * 编辑角色之---权限再回显
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionByRoleId(Integer id){
        log.info("[编辑角色回显权限]id:{}",id);
        List<Integer> permissionIds = roleService.findPermissionByRoleId(id);
        return new Result(true,"找到权限了",permissionIds);
    }

    /**
     * 编辑角色之---菜单也回显
     */
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        log.info("[编辑角色回显菜单]id:{}",id);
        List<Integer> Menu = roleService.findMenuIdsByRoleId(id);
        return new Result(true,"找到菜单了",Menu);
    }

    /**
     * 开始编辑
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        log.info("[编辑传过来的东西]role:{},permissionIds:{},menu:{}",role,permissionIds,menuIds);
        roleService.edit(role,permissionIds,menuIds);
        return new Result(true,"更新成功");
    }




}
